/*
 * This class is for the dhtmlx javascript framework,
 * to convert the data into the dhtml format data
 */

package com.advertise.tool;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.advertise.model.DHTMLXGridLine;
import com.advertise.model.TreeNode;

public class DhtmlxHelper {
	
	/*
	 * Convert the 
	 */
	public static String convertListToTreeData(List<TreeNode> treeList,int parentId){
		Document xmlDoc=DocumentHelper.createDocument();
		
		Element docRoot=xmlDoc.addElement("tree");
		docRoot.addAttribute("id", ""+parentId);
		
		
		for(TreeNode node:treeList){
			Element subNode=docRoot.addElement("item");
			
			subNode.addAttribute("text", node.getMenu());
			subNode.addAttribute("id",""+node.getId());
			
			subNode.addAttribute("child", "1");
			
			
		}
		
		return xmlDoc.asXML();
	}
	
	/*
	 * Convert the data to the dhtml grid xml data
	 */
	
	public static String convertListToGridData(List<DHTMLXGridLine> rows){
		Document xmlDoc=DocumentHelper.createDocument();
		
		Element docRoot = xmlDoc.addElement("rows");
		for(DHTMLXGridLine line:rows){
			Element row = docRoot.addElement("row");
			
			row.addAttribute("id", line.getFieldValue(0));		// add the id attribute
			
			for(int i = 1; i<line.getFields().size() ;i++){
				
				Element cell = row.addElement("cell");
				cell.setText(line.getFieldValue(i));
			}
		}
		
		return xmlDoc.asXML();
	}
}
